package Application.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


import DomainData.Reservation;
import DomainData.ReviewBean;
import Reservation.MysqlConnection;

public class ReviewDao {
	static Connection connection = null;
	static ResultSet rs = null;
	
	public static int insertReview(ReviewBean review){
		
		PreparedStatement prestatement = null;
		String sql = "INSERT INTO review (userID,Content,rating)"+"VALUES(?,?,?)";
		int userID = review.getUserID();
		int key = -1;
		String reviewContent = review.getContent();
		
		try{
			connection = MysqlConnection.getConnection();
			if(connection != null){
				prestatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
				
				prestatement.setInt(1, userID);
				prestatement.setString(2, reviewContent);
				prestatement.setInt(3, review.getRating());
				
				prestatement.executeUpdate();
				
				ResultSet keys = prestatement.getGeneratedKeys();
				keys.next();
			    key = keys.getInt(1);
			    System.out.println("FFFFFFFFFF key is "+key);
			    
			}
			else
			{
				System.out.println("**************FUCK connection is null******************");
			}
			
			connection.close();
			
			
		}
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			
		}
		return key;
	}
	
	public static void removeReview(int id){
		String sql = "DELETE FROM review WHERE ReviewID="+id;
		try{
			PreparedStatement prestatement = null;
			connection = MysqlConnection.getConnection();
			if(connection != null)
			{
				try{
					prestatement = connection.prepareStatement(sql);
					prestatement.executeUpdate();
					connection.close();
				}
				catch(SQLException e){
					e.printStackTrace();
				}
				
				
			}

		}
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		finally{
			
		}
	}
	
	public static List<ReviewBean> getReviewList(){
		List<ReviewBean> reviewlist = new ArrayList<ReviewBean>();
		
		try {
			connection = MysqlConnection.getConnection();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Statement statement = null;
		String sql = "SELECT * FROM `fkou`.review ORDER BY ReviewID ASC";
		
		try {
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			while (rs.next()) {
				ReviewBean rb = getreivewbean(rs);
				reviewlist.add(rb);
			}
			connection.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
		
		
		return reviewlist;
	}
	
	public static ReviewBean getreivewbean(ResultSet rs){
		ReviewBean rb = null;
		try{
			rb = new ReviewBean(rs.getInt("userID"),rs.getInt("ReviewID"),rs.getString("Content"));
			rb.setRating(rs.getInt("rating"));
		}
		catch(SQLException e){
			
		}
		return rb;
	}

}
